﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using Microsoft.VisualStudio.Data;
using Microsoft.Win32;
using System.IO;

namespace EffiProz.Designer
{
    [ToolboxItem(false)]
    public partial class EfzConnectionUIControl : DataConnectionUIControl
    {
        public EfzConnectionUIControl()
        {
            InitializeComponent();
        }

        #region IDataConnectionUIControl Members

        public override void LoadProperties()
        {
            string type = "file";
            string file = String.Empty;
            string user = String.Empty;
            string password = String.Empty;
            string host = String.Empty;
            string port = String.Empty;


            if (ConnectionProperties.Contains("connection type"))
                type = ConnectionProperties["connection type"] as string;
            else
                type = "file";

            if (ConnectionProperties.Contains("initial catalog"))
            {
                file = ConnectionProperties["initial catalog"] as string;               
            }

            if (ConnectionProperties.Contains("user"))
                user = ConnectionProperties["user"] as string;

            if (ConnectionProperties.Contains("host"))
                host = ConnectionProperties["host"] as string;

            if (ConnectionProperties.Contains("port"))
                port = ConnectionProperties["port"] as string;


            if (type.Equals("file", StringComparison.OrdinalIgnoreCase))
            {
                fileRadioButton.Checked = true;
                browseButton.Enabled = true;
                hostTextBox.Enabled = false;
                portTextBox.Enabled = false;
                serverGroup.Enabled = false;
                labelHost.Enabled = false;
                labelPort.Enabled = false;
            }
            else if (type.Equals("server", StringComparison.OrdinalIgnoreCase))
            {
                serverRadioButton.Checked = true;
                browseButton.Enabled = false;
                hostTextBox.Enabled = true;
                portTextBox.Enabled = true;
                serverGroup.Enabled = true;
                labelHost.Enabled = true;
                labelPort.Enabled = true;
                hostTextBox.Text = host;
                portTextBox.Text = port;
            }


            fileTextBox.Text = file;

            //if (ConnectionProperties.Contains("password"))
            //    passwordTextBox.Text = ConnectionProperties["password"] as string;
            //else
            //    passwordTextBox.Text = String.Empty;


            userTextBox.Text = user;

        }

        #endregion


        private void onChangeProperties()
        {
            ConnectionProperties["connection type"] = serverRadioButton.Checked ? "server" : "file";
            ConnectionProperties["initial catalog"] = fileTextBox.Text;
            ConnectionProperties["user"] = userTextBox.Text;
            ConnectionProperties["password"] = passwordTextBox.Text;
            if (String.Equals("file", ConnectionProperties["connection type"] as string,
                StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    ConnectionProperties.Remove("host");
                    ConnectionProperties.Remove("port");
                }
                catch (Exception) { }
            }
            else
            {
                ConnectionProperties["host"] = hostTextBox.Text;
                ConnectionProperties["port"] = portTextBox.Text;
            }

            if (fileRadioButton.Checked)
            {
                browseButton.Enabled = true;
                hostTextBox.Enabled = false;
                portTextBox.Enabled = false;
                serverGroup.Enabled = false;
                labelHost.Enabled = false;
                labelPort.Enabled = false;
            }
            else if (serverRadioButton.Checked)
            {
                browseButton.Enabled = false;
                hostTextBox.Enabled = true;
                portTextBox.Enabled = true;
                serverGroup.Enabled = true;
                labelHost.Enabled = true;
                labelPort.Enabled = true;
            }

        }

        private void securityGroup_Enter(object sender, EventArgs e)
        {
           
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = fileTextBox.Text;
            dlg.Title = "Select EffiProz Database File";

            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                fileTextBox.Text = Path.Combine(Path.GetDirectoryName(dlg.FileName), Path.GetFileNameWithoutExtension(dlg.FileName));
                fileTextBox_Leave(sender, e);
            }
        }

        private void fileTextBox_Leave(object sender, EventArgs e)
        {
            onChangeProperties();
        }

        private void userTextBox_Leave(object sender, EventArgs e)
        {
            onChangeProperties();
        }

        private void passwordTextBox_Leave(object sender, EventArgs e)
        {
            onChangeProperties();
        }

        private void databaseType_Changed(object sender, EventArgs e)
        {

            onChangeProperties();
        }
       

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
